/*
 * @Date: 2021-09-08 19:18:08
 * @Author: Acckno1
 * @LastEditTime: 2021-09-08 19:20:42
 * @Description: 
 */
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e5 + 10, M = 2 * N;

int e[N], ne[N], h[N], line[N], idx;
int n, m;
int from, to;
vector<PII> path, res_path;
bool st[N];

void add(int a, int b, int c) {
    e[idx] = b, line[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

void DFS(int u) {
    st[u] = true;
    if (u == to) {
        if (res_path.size() == 0 || path.size() < res_path.size())
        {
            res_path = path;
            
        }else if (path.size() == res_path.size()) {
            unordered_set<int> st1, st2;
            for (auto& p : path) st1.insert(p.second);
            for (auto& p : res_path) st2.insert(p.second);
            if (st1.size() < st2.size())
                res_path = path;
        }
        st[u] = false;
        return ;
    }
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (!st[j]) {
            path.push_back({e[i], line[i]});
            DFS(j);
            path.pop_back();
        }
    } 
    st[u] = false;
}

inline void solution() {
    mm(h, -1);
    cin >> n;
    // 站台 所在的 线路
    unordered_map<int, vector<int>> hash;
    for (int i = 1; i <= n; i ++ ) {
        int cnt;
        cin >> cnt;
        int sta[110];
        for (int j = 0; j < cnt; j ++ ) cin >> sta[j];
        for (int j = 0; j < cnt; j ++ ) {
            hash[sta[j]].push_back(i);
            if (j == 0) continue;
            add(sta[j], sta[j - 1], i);
            add(sta[j - 1], sta[j], i);
        }
    }

    cin >> m;
    while (m -- ) {
        path.clear();
        res_path.clear();
        mm(st, false);
        cin >> from >> to;
        DFS(from);
        int last = from;
        printf("%d\n", res_path.size()); 
        for (int i = 1; i < res_path.size(); i ++ ) {
            PII p = res_path[i];
            if (p.second != res_path[i - 1].second) {
                printf("Take Line#%d from %04d to %04d.\n", res_path[i - 1].second, last, res_path[i - 1].first);
                last = res_path[i - 1].first;
            }
        }
        if (last != to) 
            printf("Take Line#%d from %04d to %04d.\n", res_path.back().second, last, res_path.back().first);
    }
}

int main() {
//     freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}